package de.jeeyeon.util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import de.jeeyeon.R;

public final class EmoAdder 
{

	public static SpannableString setEmos(String input, Context context)
	{
		String raw = input;
		SpannableString ss = new SpannableString(raw);
		
		
		//emo_1
		Drawable img = context.getResources().getDrawable(R.drawable.emo_1);
		img.setBounds(0, 0, img.getIntrinsicWidth(), img.getIntrinsicHeight());
		
		int lastindex = -1;
		
		while(raw.contains(":-)"))
		{
			if(lastindex > 0)
			{
				int index = raw.indexOf(":-)", (lastindex));
				lastindex = (index + 3);
				
				if(index == -1)
				{
					break;
				}
				else
				{
					ss.setSpan(new ImageSpan(img, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
				}
			}
			else
			{
				int index = raw.indexOf(":-)");
				ss.setSpan(new ImageSpan(img, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
				lastindex = (index + 3);
			}
		}
		
		//emo_2
		
		Drawable img2 = context.getResources().getDrawable(R.drawable.emo_2);
		img2.setBounds(0, 0, img2.getIntrinsicWidth(), img2.getIntrinsicHeight());
		
		lastindex = -1;
		
		while(raw.contains(";-)"))
		{
			if(lastindex > 0)
			{
				int index = raw.indexOf(";-)", (lastindex));
				lastindex = (index + 3);
				
				if(index == -1)
				{
					break;
				}
				else
				{
					ss.setSpan(new ImageSpan(img2, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
				}
			}
			else
			{
				int index = raw.indexOf(";-)");
				ss.setSpan(new ImageSpan(img2, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
				lastindex = (index + 3);
			}
		}
		
		//emo_3
		Drawable img3 = context.getResources().getDrawable(R.drawable.emo_3);
		img3.setBounds(0, 0, img3.getIntrinsicWidth(), img3.getIntrinsicHeight());
		
		lastindex = -1;
		
		while(raw.contains(";-P"))
		{
			if(lastindex > 0)
			{
				int index = raw.indexOf(";-P", (lastindex));
				lastindex = (index + 3);
				
				if(index == -1)
				{
					break;
				}
				else
				{
					ss.setSpan(new ImageSpan(img3, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
				}
			}
			else
			{
				int index = raw.indexOf(";-P");
				ss.setSpan(new ImageSpan(img3, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
				lastindex = (index + 3);
			}
		}
		
		//emo_4
		
		Drawable img4 = context.getResources().getDrawable(R.drawable.emo_4);
		img4.setBounds(0, 0, img4.getIntrinsicWidth(), img4.getIntrinsicHeight());
		
		lastindex = -1;
		
		while(raw.contains(":-J"))
		{
			if(lastindex > 0)
			{
				int index = raw.indexOf(":-J", (lastindex));
				lastindex = (index + 3);
				
				if(index == -1)
				{
					break;
				}
				else
				{
					ss.setSpan(new ImageSpan(img4, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
				}
			}
			else
			{
				int index = raw.indexOf(":-J");
				ss.setSpan(new ImageSpan(img4, ImageSpan.ALIGN_BASELINE), index, index + 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
				lastindex = (index + 3);
			}
		}
		return ss;
	}
	
}
